<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发货通知单号" prop="consignmentId">
        <el-input v-model="queryParams.consignmentId" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <CustomSearch ref="ClientSelect2" label="客户名称" prop="clientName" labelWidth="110px" :tableKeys="ClientTableKeys2"
        formTitle="选择客户" primaryKey="clientName" showField="clientName" @getSearchList="getlistClient2"
        @updateQueryParams="updateQueryParamsForClient2" @selectDialogItem="selectDialogItemForClient2"></CustomSearch>
      <el-form-item label="规格型号" prop="specifications">
        <el-select v-model="queryParams.specifications" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.specification_and_model" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="queryParams.productName" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户型号" prop="customerModel">
        <el-select v-model="queryParams.customerModel" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.customer_model" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="销售订单号" prop="clientOrderNumber" label-width="85px">
        <el-input v-model="queryParams.clientOrderNumber" placeholder="请输入销售订单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="颜色" prop="colour">
        <el-select v-model="queryParams.colour" filterable placeholder="请选择颜色" clearable>
          <el-option v-for="dict in dict.type.colour" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="同步状态" prop="synchState">
        <el-select v-model="queryParams.synchState" placeholder="请选择审核状态" clearable>
          <el-option v-for="dict in dict.type.sync_state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="sync"
          v-hasPermi="['system:consignment:sync ']">同步</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" ref="multipleTable" :data="consignmentList" @select="handleSelect"
      @select-all="handleSelectAll">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出库类型" align="center" prop="types">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.outbound_types" :value="scope.row.types" />
        </template>
      </el-table-column>
      <el-table-column label="入库单号" align="center" prop="warehousingId" />
      <el-table-column label="云星空单号" align="center" prop="skyCoding" />
      <el-table-column label="销售订单号" align="center" prop="clientOrderNumber" />
      <el-table-column label="产品编码" align="center" prop="productId" />
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="规格型号" align="center" prop="specifications">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.specification_and_model" :value="scope.row.specifications" />
        </template>
      </el-table-column>
      <el-table-column label="客户型号" align="center" prop="customerModel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.customer_model" :value="scope.row.customerModel" />
        </template>
      </el-table-column>
      <el-table-column label="颜色" align="center" prop="colour">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.colour" :value="scope.row.colour" />
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="出货仓库" align="center" prop="warehouseId" />
      <el-table-column label="出库数量" align="center" prop="weekdayed" />
      <el-table-column label="同步状态" align="center" prop="synchState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sync_state" :value="scope.row.synchState" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


  </div>
</template>

<script>
import { listDataBackhaul, syncDataBackhaul, getDataBackhaulNumber } from "@/api/outbound/dataBackhaul/dataBackhaul";
import { ClientSelect2 } from '@/mixins/client/cliemtSelectForQueryParams.js'
import CustomSearch from "@/views/commons/CustomSearch.vue";
export default {
  name: "dataBackhaul",
  mixins: [ClientSelect2],
  components: { CustomSearch },
  dicts: ["outbound_types", "sync_state", "colour", "customer_model", "specification_and_model"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发货通知单表格数据
      consignmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      selectedRows: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientOrderNumber: null,
        date: null,
        clientName: null,
        productName: null,
        audit: null,
        close: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询发货通知单列表 */
    getList() {
      this.loading = true;
      listDataBackhaul(this.queryParams).then(response => {
        this.consignmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        consignmentId: null,
        types: null,
        clientOrderNumber: null,
        date: null,
        clientName: null,
        productId: null,
        productName: null,
        color: null,
        warehouseId: null,
        weekdaying: null,
        weekdayed: null,
        returnNumber: null,
        audit: null,
        close: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectAll(selection) {
      // 如果全选，则选中所有行  
      if (this.selectedRows.length == 0) {
        this.selectedRows = [...this.consignmentList];
      } else {
        // 如果取消全选，则清除选中状态  
        this.selectedRows = [];
      }
      if (this.consignmentList.length == this.selectedRows.length) { }
      // 同步选中状态到表格（如果有需要的话）  
      this.$refs.multipleTable.clearSelection();
      this.selectedRows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row, true);
      });
    },
    handleSelect(selection, row) {
      // 清除之前的选中状态  
      // this.selectedRows = [];
      // 找到所有相同类型的行，并添加到选中状态数组中
      let a = 0;
      let b = false;
      this.selectedRows.forEach(function (item, index) {
        if (item.containerNumber == row.containerNumber) {
          a = index;
          b = true;
          return;
        }
      })
      if (b) {
        this.selectedRows.splice(a, 1)
      } else {
        this.consignmentList.forEach(item => {
          if (item.consignmentId === row.consignmentId) {
            this.selectedRows.push(item);
          }
        });
      }

      // 同步选中状态到表格（如果有需要的话）  
      this.$refs.multipleTable.clearSelection();
      this.selectedRows.forEach(row => {
        this.$refs.multipleTable.toggleRowSelection(row, true);
      });
    },
    //同步操作
    sync(row) {
      const consignmentIds = row.consignmentId || this.ids
      console.log(consignmentIds);
      syncDataBackhaul(consignmentIds)
    }
  }
};
</script>
